package com.yidong;

import com.yidong.entity.YiDongObj;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.*;

import java.io.*;

public class FuKuanWX {

    public static String fksm_path = "D:\\code\\Projects\\yidong\\src\\main\\java\\com\\yidongcfg\\wuxi付款模板\\付款说明.xlsx";
    public static String zfsqd_path = "D:\\code\\Projects\\yidong\\src\\main\\java\\com\\yidongcfg\\wuxi付款模板\\支付申请单.docx";
    public static String yhzh_path = "D:\\code\\Projects\\yidong\\src\\main\\java\\com\\yidongcfg\\wuxi付款模板\\银行账户确认函.docx";


    public static String fksm_out = "D:\\code\\Projects\\yidong\\src\\main\\java\\com\\yidongcfg\\付款说明.xlsx";
    public static String zfsqd_out = "D:\\code\\Projects\\yidong\\src\\main\\java\\com\\yidongcfg\\支付申请单.docx";
    public static String yhzh_out = "D:\\code\\Projects\\yidong\\src\\main\\java\\com\\yidongcfg\\wuxi付款模板.docx";



    //wuxi方法汇总
    public void WuXifukuan(YiDongObj obj){
        fuKuanShuoMing(obj);
        zhiFuShenQing(obj);
        yinHangZhangHu(obj);
    }

    /**
     * 付款说明 .xlsx
     * @param obj
     */
    public void fuKuanShuoMing(YiDongObj obj){
        try (FileInputStream fis = new FileInputStream(new File(fksm_path));
             Workbook workbook = new XSSFWorkbook(fis)) {

            Sheet sheet = workbook.getSheetAt(0); // 获取第一个Sheet，索引从0开始

            for (Row row : sheet) {
                for (Cell cell : row) {
                    // 只在文本类型的单元格中查找和替换
                    if (cell.getCellType() == CellType.STRING) {
                        String originalText = cell.getStringCellValue();
                        String runText = originalText;

                        //替换
                        runText = runText.replaceAll("%项目名称%",obj.getXmmc());
                        runText = runText.replaceAll("%合同金额%",obj.getHtzjefk());
                        runText = runText.replaceAll("%合同编号%",obj.getHtbhfk());
                        runText = runText.replaceAll("%付款金额%",obj.getFkje());
                        runText = runText.replaceAll("%合同金额前%",obj.getHtjeQ());

                        // 如果替换后的文本与原始文本不同，则更新单元格
                        if (!originalText.equals(runText)) {
                            cell.setCellValue(runText);
                        }
                    }
                }
            }

            // 将修改后的Workbook写入到新文件
            try (FileOutputStream fos = new FileOutputStream(fksm_out)) {
                workbook.write(fos);
                System.out.println("Excel文件已成功保存为: " + fksm_out);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 支付申请单 .doc
     * @param obj
     */
    public void zhiFuShenQing(YiDongObj obj){
        try (InputStream is = new FileInputStream(zfsqd_path);
             XWPFDocument document = new XWPFDocument(is)) {

            // 遍历文档中的所有段落
            for (XWPFParagraph paragraph : document.getParagraphs()) {
                for (XWPFRun run : paragraph.getRuns()) {
                    String runText = run.text();

                    //替换
                    runText = runText.replaceAll("%项目名称%",obj.getXmmc());

                    run.setText(runText, 0);
                }
            }

            // 遍历文档中的所有表格
            for (XWPFTable table : document.getTables()) {
                for (XWPFTableRow row : table.getRows()) {
                    for (XWPFTableCell cell : row.getTableCells()) {
                        for (XWPFParagraph cellParagraph : cell.getParagraphs()) {
                            for (XWPFRun cellRun : cellParagraph.getRuns()) {
                                String runText = cellRun.text();

                                //替换
                                runText = runText.replaceAll("%支付类别%",obj.getZflb());
                                runText = runText.replaceAll("%项目名称%",obj.getXmmc());
                                runText = runText.replaceAll("%项目编号%",obj.getXmbh());
                                runText = runText.replaceAll("%合同名称%",obj.getHtmcfk());
                                runText = runText.replaceAll("%合同编号%",obj.getHtbhfk());
                                runText = runText.replaceAll("%合同金额%",obj.getHtzjefk());
                                runText = runText.replaceAll("%合同签订日期%",obj.getHtqdrq());
                                runText = runText.replaceAll("%付款条件%",obj.getFktj());
                                runText = runText.replaceAll("%本次付款金额%",obj.getFkje());
                                runText = runText.replaceAll("%日期%",obj.getToday());

                                cellRun.setText(runText, 0);
                            }
                        }
                    }
                }
            }

            // 保存文档到新文件
            try (OutputStream out = new FileOutputStream(zfsqd_out)) {
                document.write(out);
            }
            System.out.println("文档已更新并保存为: " + zfsqd_out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 银行账户确认函 docx
     * @param obj
     */
    public void yinHangZhangHu(YiDongObj obj){
        try (InputStream is = new FileInputStream(yhzh_path);
             XWPFDocument document = new XWPFDocument(is)) {

            // 遍历文档中的所有段落
            for (XWPFParagraph paragraph : document.getParagraphs()) {
                for (XWPFRun run : paragraph.getRuns()) {
                    String runText = run.text();

                    //替换
                    runText = runText.replaceAll("%合同名称%",obj.getHtmcfk());
                    runText = runText.replaceAll("%日期%",obj.getToday());

                    run.setText(runText, 0);
                }
            }

            // 保存文档到新文件
            try (OutputStream out = new FileOutputStream(yhzh_out)) {
                document.write(out);
            }
            System.out.println("文档已更新并保存为: " + yhzh_out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
